/**
*    This file is part of Basic Service.
*
*    Basic Service is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    Basic Service is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with Basic Service (See GPL.txt).  If not, see <http://www.gnu.org/licenses/>.
*    
* 	If needed the author is Amir Zucker and can be reached at amirzucker1@gmail.com or amirzucker.wordpress.com
*/
package com.basicservice.domain;

import java.util.Date;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import com.basicservice.domain.ActionValidation.TYPE;
import com.basicservice.service.Utils;

/**
 * Used to hold a validation key for specific user and action
 */

@Document
public class ActionValidation {
	@Id
	private String key;
	private String userId;
	private Date creationDate;
	private TYPE type;
	
	/**
	 * @param key
	 * @param userId
	 * @param type
	 */
	public ActionValidation(String key, String userId, TYPE type) {
		this.key = key;
		this.userId = userId;
		this.type = type;
		this.creationDate = new Date();
	}
	
	public ActionValidation() {
	}

	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public Date getCreationDate() {
		return creationDate;
	}
	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}
	public TYPE getType() {
		return type;
	}
	public void setType(TYPE type) {
		this.type = type;
	}

	public static enum TYPE {
		EMAIL_VALIDATION(60*60*24*3), // valid for 3 days
		PASSWORD_RESET(60*60*3); // valid for 3 hours 

		int validFor;  // in seconds
		
		TYPE(int validFor) {
			this.validFor = validFor;
		}
		
		public long getValidFor() {
			return validFor;
		}
	}

	public boolean isValid(TYPE type) {
		return this.type == type &&
				Utils.getCurrentTimeInSeconds() - (creationDate.getTime() / 1000) < type.getValidFor();
	}
}
